package com.ooqiu.gaming.ui.main.thread.index.content;

import com.ooqiu.gaming.api.entities.Article;
import com.ooqiu.gaming.api.repository.InformationRepository;
import com.ooqiu.gaming.injector.FragmentScope;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.observers.DefaultObserver;

/**
 * @author Eenie
 * @date 2017/11/22
 */

@FragmentScope
public class IndexContentPresenter implements IndexContentContract.Presenter {
    private InformationRepository mInformationRepository;
    private IndexContentContract.View mView;

    @Inject
    public IndexContentPresenter(InformationRepository informationRepository, IndexContentContract.View view) {
        mInformationRepository = informationRepository;
        this.mView = view;
    }

    @Override
    public void fetchArticleList(final int page, int typeId, final boolean isRefresh) {
        mInformationRepository.fetchNewsList(page, typeId)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new DefaultObserver<List<Article>>() {
                    @Override
                    public void onNext(List<Article> listReply) {
                        mView.onFetchArticleList(listReply, isRefresh);
                        mView.onRefreshComplete();
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        mView.onRefreshComplete();
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
